package com.card.hwatt.assembler;

import java.util.ArrayList;
import java.util.List;

import com.card.hwatt.core.ObjectUtil;
import com.card.hwatt.dto.PageResult;
import com.card.hwatt.entity.Employee;

/**
 * 用户
 * @author Administrator
 *
 */
public class UserAssembler {

	public static PageResult<com.card.hwatt.dto.User> convert(
			PageResult<Employee> list) {
		PageResult<com.card.hwatt.dto.User> result = new PageResult<com.card.hwatt.dto.User>();
		List<com.card.hwatt.dto.User> rows = new ArrayList<com.card.hwatt.dto.User>();
		result.rows = rows;
		for (Employee item : list.rows) {
			com.card.hwatt.dto.User dto = convert(item);
			rows.add(dto);
		}
		result.total = list.total;
		return result;
	}

	public static com.card.hwatt.dto.User convert(Employee entity) {
		com.card.hwatt.dto.User dto = new com.card.hwatt.dto.User();
		ObjectUtil.fieldCopy(entity, dto);
		
		dto.isAdmin = entity.user != null && entity.user.isAdmin;
		dto.id = entity.user == null ? null : entity.user.id;
		dto.password = entity.user == null ? null : entity.user.password;
		
		dto.employeeId = entity.id;
		dto.employeeCode = entity.employeeCode;
		dto.departmentId = entity.department.id;
		dto.departmentName = entity.department.name;

		return dto;
	}

	public static com.card.hwatt.entity.User convert(com.card.hwatt.dto.User dto) {
		com.card.hwatt.entity.User entity = new com.card.hwatt.entity.User();
		ObjectUtil.fieldCopy(dto, entity);
		entity.employee = new Employee();
		entity.employee.id = dto.employeeId;
		return entity;
	}
}
